﻿using System;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class GlobalSettings : Form
    {
        public GlobalSettings()
        {
            InitializeComponent();
        }

        private EventScavengerDAL sqlEventSource;

        #region Public methods
        public DialogResult ShowSettings(EventScavengerDAL sqlEventSource)
        {
            this.sqlEventSource = sqlEventSource;
            
            return this.ShowDialog();
        } 
        #endregion

        #region Form events
        private void GlobalSettings_Load(object sender, EventArgs e)
        {
            string lastStep = "Load collectors";
            try
            {
                foreach (Collector collector in sqlEventSource.GetCollectorList())
                {
                    cboMaintenanceRunner.Items.Add(collector.Name);
                }

                lastStep = "Reading all global settings";
                GlobalSettingsData globalSettingsData = sqlEventSource.GetGlobalSettings();

                lastStep = "Get global max entries";
                maxLogEntriesNumericUpDown.Value = globalSettingsData.MaxLogEntries;
                lastStep = "Is maintenance enabled";
                checkBoxRunMaintenance.Checked = globalSettingsData.MaintenanceEnabled;
                lastStep = "Get maintenance frequency";
                numericUpDownMaintenanceFrequency.Value = globalSettingsData.MaintenanceFrequencyMinutes;
                lastStep = "Get maintenance runner";
                cboMaintenanceRunner.Text = globalSettingsData.MaintenanceRunner;
                lastStep = "When was last maintenance run";
                lblMaintenanceLastRun.Text = globalSettingsData.MaintenanceLastRun.ToString("yyyy-MM-dd HH:mm");
                lastStep = "Maintenance run time-out time";
                numericUpDownMaintenanceTimeOut.Value = globalSettingsData.MaintenanceTimeOutSeconds;
                lastStep = "When was last create history run";
                dateTimeChooserCreateHistory.SelectedDateTime = globalSettingsData.NextCreateHistoryTime;
                lastStep = "Is create history enabled";
                chkCreateHistoryEnabled.Checked = globalSettingsData.RunCreateHistoryEnabled;
                lastStep = "Get create history frequency";
                createHistoryMinutesNumericUpDown.Value = globalSettingsData.NextCreateHistoryFrequencyMinutes;
                lastStep = "Get 'days to history'";
                numericUpDownDaysToHistory.Value = globalSettingsData.DaysToHistory;
                lastStep = "Get duplicate entry repeat time";
                numericUpDownRepeatTime.Value = globalSettingsData.RepeatTimeSec;
            }
            catch (Exception ex)
            {
                cmdOK.Enabled = false;
                MessageBox.Show("Last step: " + lastStep + "\r\n" + ex.Message + "\r\n" + sqlEventSource.LastError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Button events
        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxRunMaintenance.Checked)
                {
                    if (cboMaintenanceRunner.SelectedIndex == -1)
                    {
                        MessageBox.Show("You must specify which collector will run the maintenance!", "Maintenance", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        cboMaintenanceRunner.Focus();
                        return;
                    }
                }

                GlobalSettingsData globalSettingsData = new GlobalSettingsData()
                {
                    MaxLogEntries = (int)maxLogEntriesNumericUpDown.Value,
                    MaintenanceEnabled = checkBoxRunMaintenance.Checked,
                    MaintenanceFrequencyMinutes = (int)numericUpDownMaintenanceFrequency.Value,
                    MaintenanceRunner = cboMaintenanceRunner.Text,
                    MaintenanceTimeOutSeconds = (int)numericUpDownMaintenanceTimeOut.Value,
                    NextCreateHistoryTime = dateTimeChooserCreateHistory.SelectedDateTime,
                    RunCreateHistoryEnabled = chkCreateHistoryEnabled.Checked,
                    NextCreateHistoryFrequencyMinutes = (int)createHistoryMinutesNumericUpDown.Value,
                    DaysToHistory = (int)numericUpDownDaysToHistory.Value,
                    RepeatTimeSec = (int)numericUpDownRepeatTime.Value
                };
                sqlEventSource.SetGlobalSettings(globalSettingsData);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (System.Data.SqlClient.SqlException seqex)
            {
                MessageBox.Show(seqex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + sqlEventSource.LastError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }         
        #endregion

        #region Checkbox events
        private void checkBoxRunMaintenance_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownMaintenanceFrequency.Enabled = checkBoxRunMaintenance.Checked;
            numericUpDownMaintenanceTimeOut.Enabled = checkBoxRunMaintenance.Checked;
            cboMaintenanceRunner.Enabled = checkBoxRunMaintenance.Checked;
            lblMaintenanceLastRun.Enabled = checkBoxRunMaintenance.Checked;
        } 
        private void chkCreateHistoryEnabled_CheckedChanged(object sender, EventArgs e)
        {
            dateTimeChooserCreateHistory.Enabled = chkCreateHistoryEnabled.Checked;
            createHistoryMinutesNumericUpDown.Enabled = chkCreateHistoryEnabled.Checked;
            numericUpDownDaysToHistory.Enabled = chkCreateHistoryEnabled.Checked;
        }
        #endregion

    }
}
